export const formatCategory = (category = {}, key) => ({
  key, // 可能没有id，title变化的时候会重绘dom
  id: category.id,
  title: category.title,
});

export const formatCategories = categories => categories
  .sort((a, b) => Number(a.index) - Number(b.index))
  .map((category, i) => formatCategory(category, i));

export const formatCategoriesAddAll = (categories = []) => [{ title: '全部' }].concat(formatCategories(categories)); // ’全部‘是为了识别已经请求到，在处理的时候要删掉

export const formatVoteStates = (voteStates = {}) => ({
  collect: voteStates.collection,
  like: voteStates.like,
  dislike: voteStates.dis_like,
});

export const formatVoteCounts = (voteCounts = {}) => ({
  collect: voteCounts.collection_size || 0,
  like: voteCounts.like_size || 0,
  dislike: voteCounts.dis_like_size || 0,
});

export const formatFileSize = (size = 0) => {
  let result = Number(size);
  if (result < 1024) return `${result.toFixed(2)} B`;
  result /= 1024;
  if (result < 1024) return `${result.toFixed(2)} KB`;
  return `${(result / 1024).toFixed(2)} MB`;
};

export const formatDisplayPath = (path = '') => path
  .split('/')
  .map((itemStr) => {
    if (itemStr) return itemStr.substr(itemStr.indexOf('-') + 1);
    return '根目录';
  })
  .join('/');
